package linter.linterjava.controller.model;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.linterjava.POJO.DTO.UserDTO;
import linter.linterjava.POJO.UtilDTO.ModelUploadUtilDTO;
import linter.linterjava.POJO.UtilDTO.TypeDTO;
import linter.linterjava.custom.ResultVO;
import linter.linterjava.service.ModelUserDataService;
import linter.linterjava.service.UserQueryService;
import linter.linterjava.service.UserService;
import linter.linterjava.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static linter.linterjava.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@RestController
@Api(tags = "模型训练模块")
@RequestMapping("modelTrain")
public class ModelTrainController {

    @Resource
    UserService userService;
    @Resource
    ModelUserDataService modelUserDataService;
    private final UserQueryService userQueryService;
    @Autowired
    public ModelTrainController(UserQueryService userQueryService) { this.userQueryService = userQueryService; }
    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) { return userQueryService.getUserByUsername(username); }


    @PostMapping("LinearRegression")
    @ApiOperation("线性回归模型")
    public ResultVO<String> linearRegression(@RequestBody ModelUploadUtilDTO modelUploadUtilDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(modelUserDataService.linearRegression(modelUploadUtilDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

}
